<?php
defined('_JEXEC') or die( 'Restricted access' );

class TablePlantRecord extends Fcore
{
	/** @var int Primary key */
	var $id					= 0;
	/** @var int */
	var $ppid				= '';
	/** @var date */
	var $date				= '0000-00-00';
	/** @var int */
	var $uid				= 0;
	/** @var string */
	var $comment			= '';
	/** @var string */
	var $dids				= '';
	/** @var string */
	var $status				= '';
	
	/**
	* @param database A database connector object
	*/
	function __construct( &$db )
	{
		parent::__construct( '#__farm_plant_record', 'id', $db );
	}

	/**
	 * Binds an array to the object
	 * @param 	array	Named array
	 * @param 	string	Space separated list of fields not to bind
	 * @return	boolean
	 */
	function bind( $array, $ignore='' )
	{
		$result = parent::bind( $array );
		// cast properties
		$this->id	= (int) $this->id;
		return $result;
	}

	/**
	 * Overloaded check function
	 *
	 * @access public
	 * @return boolean
	 * @see JTable::check
	 * @since 1.5
	 */
	function check()
	{
		if (!intval( $this->ppid ))
		{
			$this->setError(JText::_( '请选择种植计划' ));
			return false;
		}
		
		if (!trim( $this->date ))
		{
			$this->setError(JText::_( '请指定实施日期' ));
			return false;
		}
		
		if (!intval( $this->uid ))
		{
			$this->setError(JText::_( '请选择记录人员' ));
			return false;
		}
		
		if (is_array( $this->dids ) && !empty($this->dids))
		{
			$this->dids = '|' . join('|', $this->dids) . '|';
		} else $this->dids = '';
		
		$this->status = trim($this->status);
		
		return true;
	}	
	
	/**
	 * 创建新的种植记录
	 */
	public function newPlantRecord()
	{
		//保存基本信息
		if (!$this->save($_POST)) die('1');
		
		//上一条插入数据ID
		$prid = $this->getLastInsertID();
		
		if (!$prid) die('1');
		
		//保存操作信息
		$res = $this->savePlanExe($prid);
		
		return $res;
	}
	
	/**
	 * 保存种植具体信息
	 * @param int $prid 种植记录ID
	 * @return boolean
	 */
	private function savePlanExe($prid)
	{
		//分析POST数据
		$sql = "UPDATE #__farm_plan SET prid=$prid, 
			execution_time='" . date('Y-m-d') . "' 
			WHERE id IN (" . implode(',', $_POST['ppoid']) . ")";
		
		return $this->excuteQuery($sql);
	}
	
	/**
	 * 种植计划名及ID
	 */
	public function dataForOptions()
	{
		$query = 'SELECT pp.id, CONCAT(f.no, "-", p.name, "-", u.name) AS no FROM ' . $this->_tbl . ' AS pp 
			LEFT JOIN #__users AS u ON u.id=pp.uid 
			LEFT JOIN #__farm_field AS f ON f.id=pp.fid 
			LEFT JOIN #__farm_plant AS p ON p.id=pp.pid';
		$this->_db->setQuery( $query );
		if (!$this->_db->query())$this->_error .= $this->_db->getErrorMsg() . "\n";		
		return $this->_db->loadAssocList();
	}
	
	public function prlist()
	{		
		$data = $this->alist();
		if (empty($data[1])) return $data;
		
		$tDi		= JTable::getInstance('disease', 'Table');
		$disease	= $this->getOpt($tDi->getAll('', 'id, name'), 'id', 'name');
		
		//用户名
		$db	= JFactory::getDBO();
		$query = 'SELECT id, name no' .	' FROM #__users';
		$db->setQuery($query);
		$userArr = $this->getOpt($db->loadAssocList(), 'id', 'no');
		
		$tmpPP = $tmpPPo = array();
		$tPP	= JTable::getInstance('plantPlan', 'Table');
		foreach ((array)$data[1] as $k => $v) {
			if (!isset($tmpPP[$v->ppid])) 
				$tmpPP[$v->ppid] = $tPP->getInfo($v->ppid);
			
			$didArr = explode('|', trim($v->dids, '|'));
			if (is_array($didArr)) {
				foreach ($didArr as $k2 => $v2) $didArr[$k2] = $disease[$v2];
			}

			$data[1][$k] = array(
				'dids'		=> implode('<br />', $didArr),
				'uid'		=> $userArr[$v->uid],
				'comment'	=> $v->comment,
				'status'	=> $v->status,
				'date'		=> $v->date,
				'ppid'		=> $tmpPP[$v->ppid],
				'ppoid'		=> $tPP->getOpes($v->ppid, $v->id),
				'id'		=> $v->id
			);
		}		
		return $data;
	}
}
